//
//  AppDelegate.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/15.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        JWLog(NSHomeDirectory())
        JWLog(NSHomeDirectory())
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        

        //创建window
        window = UIWindow(frame:UIScreen.mainScreen().bounds)
        
//        let sb = UIStoryboard(name: "NEWfeatureController", bundle: nil)
//        let sbVC = sb.instantiateInitialViewController()
        
        window?.rootViewController = defaultViewController()
        
        //显示window
        window?.makeKeyAndVisible()

        JWLog(isNewVersion())
        
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.notification), name: SwithrootViewController, object: nil)
        
        return true
    }
}

//检查版本更新

extension AppDelegate{
    
    
    func notification(notifi:NSNotification) {
        if (notifi.object as! NSString).isEqualToString("main"){
            
          window?.rootViewController =  UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateInitialViewController()!
        }
        else if (notifi.object as! NSString).isEqualToString("welcome"){
            
            window?.rootViewController =  UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()!
            return
        }
    }
    
    //跳转到哪个界面,在这里判断
    func defaultViewController() -> UIViewController {
        
        //用户是否登录?
        if  UserAccount.isLogin(){
            //是否刚跟新版本:
            if isNewVersion(){//是的话显示新版本界面
                return UIStoryboard(name: "NEWfeatureController", bundle: nil).instantiateInitialViewController()!
            }
            else{//是否刚跟新版本:否,显示欢迎回来界面
                
                return UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()!
                
            }
        }
        
        else{//如果用户没有登录:直接显示main
            return UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateInitialViewController()!
            
            
        }
        
        
        
        
    }
    
    
    
    
    //判断是否有版本更新
    func isNewVersion() ->Bool {
        //1.加载info里面的版本信息,获取当前软件版本号
       let currentVersion =  NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! NSString
        //2.获取以前软件的版本号
        let defaults = NSUserDefaults.standardUserDefaults()
        let sanboxVersion = (defaults.objectForKey("xx") as? String) ?? "0.0"
        //3.用当前的版本号和以前的版本号进行对比
        //1.0   0.0
        //如果是降序,说明当前的大于以前的, 有新版本
        if currentVersion.compare(sanboxVersion) == NSComparisonResult.OrderedDescending {
            JWLog("有新版本")
            // 如果有新版本, 就利用新版本的版本号更新本地的版本号
            defaults.setObject(currentVersion, forKey: "xx")
            defaults.synchronize() // iOS7以前需要写, iOS7以后不用写
            return true
        }
        
        //否则,没有新版本
        JWLog("没有新版本")
        return false
        
        
    }
}

func JWLog<T>(message: T, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line)
{
    #if DEBUG
        //    print("\((fileName as NSString).pathComponents.last!).\(methodName)[\(lineNumber)]:\(message)")
        print("\((fileName as NSString).pathComponents.last!)\(methodName)[\(lineNumber)]:\(message)")
    #endif
}

